(function(){
    function SetCustomInfo(){
        this.init(); 
    }

    SetCustomInfo.prototype = {
        constructor:SetCustomInfo,
        init:function(){
            this.nodata = '<tr><td  colspan="7">暂无信息</td></tr>';
            var _this = this;
        
                this.getReportList(1);
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getReportList(1);
                });

                $.extend({page:function(pn){
                    _this.getReportList(pn);
                }});
                // 添加用户
                this.addNewUser();
        },
        getReportList:function( pn ){
            var _this = this;
            var searchName = $("#username").val();
            if( searchName )
            {
                searchName = "&username="+searchName;
            }
            $.ajax({
                type:"get",
                url:"/admin/users?ps=20&pn="+pn+searchName,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        var pageno = count.pn;
                        if( data.length )
                        {
                            var strArr = [];
                            var openJson = {
                                "0":"普通用户",
                                "1":"白名单"
                            };
                             // 投注的票是否分到门店去
                            var splitJson = {
                                "0":"否",
                                "1":"是"
                            };
                           
                           
                            data.forEach(function( value, key  ){
                                var username  = value.username ;
                                var open = value.open;//0 普通用户 1白名单
                                var balance = value.balance;//余额
                                var split = value.split;//余额
                                var stime = value.stime.substring(0,16);//注册时间
                                var etime = value.etime.substring(0,16);//最近活动时间
                                 
                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td>'+((pageno-1)*20+key+1)+'</td>\
                                                <td>'+username+'</td>\
                                                <td  title="普通用户无法投注，白名单可以投注">'+openJson[open]+'</td>\
                                                <td>'+splitJson[split]+'</td>\
                                                <td>'+balance+'</td>\
                                                <td>'+stime+'</td>\
                                                <td>'+etime+'</td>\
                                                <td>\
                                                <a href="javascript:;" class="btn btn-info  addmoneyBtn" data-uname="'+username+'"   data-pageno="'+pageno+'">加款</a>\
                                                <a href="javascript:;" class="btn btn-info  cutdownBtn" data-uname="'+username+'"  data-pageno="'+pageno+'">扣款</a>\
                                                 <a href="javascript:;" class="btn btn-info  editorBtn" data-uname="'+username+'" data-open="'+open+'"  data-esplit="'+split+'"    data-pageno="'+pageno+'" >编辑</a>\
                                                 <a href="customflow.html?cuname='+username+'" class="btn btn-info  showliushuiBtn">流水</a>\
                                                 </td>\
                                            </tr>';

                                    strArr.push( html );
                            });

                            $("#unticketList").html( strArr.join(""));
                          
                            
                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);
                            // 编辑用户
                            $(".editorBtn").unbind("click").click(function(){
                                var uname = $(this).data("uname");
                                var open = $(this).data("open");
                                var split = $(this).data("esplit");
                                _this.pagenum = $(this).data("pageno");
                                $("#editusername").val(uname);
                                $("#edituserpwd").val(open);
                                $("#editfenpiao").val(split);
                                $("#editorUserModal").modal();
                            });

                            $("#editUserBtn").unbind().click(function(){
                                _this.editorCustom();
                            });
                            // 加款
                            $(".addmoneyBtn").click(function(){
                                $("#addmoneyCustomModal").modal();
                                _this.addMoneyCustomInfo(this);

                             });
                              // 扣款
                            $(".cutdownBtn").click(function(){
                                $("#cutdownmoneyCustomModal").modal();
                                _this.cutMoneyCustomInfo(this);

                             });
                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        addNewUser:function(){
            var _this = this;
            $("#addNewUser").click(function(){
                $("#addUserModal").modal();
            });
            $("#addUserBtn").unbind().click(function(){
                _this.doAddUser();

            });

        },
        doAddUser:function(){
            var _this = this;
            var newName = $("#addusername").val();
            var newpwd = $("#adduserpwd").val();
            if( !newName )
            { 
                $("#addusername").focus();
                return false;
            }
            if( !newpwd )
            { 
                $("#adduserpwd").focus();
                return false;
            }
            $.ajax({
                type:"get",
                url:"/admin/add_admin_user?username="+newName+"&password="+newpwd,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#addUserBtn").popover("show");
                        $("#addusername,#adduserpwd").val("");
                        _this.getReportList(1);
                        setTimeout(function(){
                           $("#addUserBtn").popover("hide");
                           $("#addUserModal").modal("hide");
                       }, 800);
                    }
                    else
                    {
                         $("#addUserModal").modal("hide");
                             if( code == 2 )
                             {
                               
                                $("#tipsContent").html("你还未登录");
                                $("#loginoutModal").modal();
                                setTimeout(function(){
                                    location.href = "login.html";
                                }, 800);
                             }
                             else
                             {
                                $("#tipsContent").html(desc);
                                $("#loginoutModal").modal();

                             }
                    }

                }
            });
        },
        // 编辑用户
        editorCustom:function(){
            var _this = this;
            var uname = $("#editusername").val();
            var open = $("#edituserpwd").val();
            var newSplit = $("#editfenpiao").val();
            if(!uname)
            {
                $("#editusername").focus();
                return false;
            }
            if(!open)
            {
                $("#edituserpwd").focus();
                return false;
            }
            if(!newSplit)
            {
                $("#editfenpiao").focus();
                return false;
            }
            $.ajax({
                type:"get",
                url:"/admin/update_user?username="+uname+"&open="+open,
                dataType:"json",
                success:function( info ){
                      var code = info.code;
                      var desc = info.desc;
                      if( code==1)
                      {
                          var currentpage = _this.pagenum;
                          $("#editUserBtn").popover("show");
                          _this.getReportList(currentpage);
                         setTimeout(function(){
                            $("#editUserBtn").popover("hide");
                            $("#editorUserModal").modal("hide");
                         }, 800);

                      }
                      else
                      {
                         $("#editorUserModal").modal("hide");
                         $.alert(code, desc);

                      }


                }

            });


        },
        //商户加款
        addMoneyCustomInfo:function( that ){
            var uname  = $(that).data("uname");   
            var pageNo = parseInt( $(that).data("pageno") );
            $("#addmoneyCustomNum").val(uname);
            var _this = this;
            $("#addmoneyCustomBtn").unbind("click").click(function(){
                 var addmoney  =  $("#addtotalmomey").val();
                 var mark = $("#addmoneymark").val();
                 if( !addmoney )
                 {
                     $("#addtotalmomey").focus();
                     return false;
                 }
                 var pData = {
                     username:uname,
                     money:addmoney,
                     remark:mark
                 };
                 $.ajax({
                    type:"post",
                    url:"/admin/add_money",
                    data:pData,
                    success:function( info ){
                        var code = info.code;
                        var desc = info.desc;
                        if( code == 1 )
                        {
                            _this.getReportList(pageNo);
                            $("#addmoneyCustomBtn").popover("show");
                            setTimeout(function(){
                                $("#addmoneyCustomBtn").popover("hide");
                                $("#addmoneyCustomModal").modal("hide");
                            }, 300);

                        }
                        else
                        {
                            $("#addmoneyCustomModal").modal("hide");
                            $.alert(code, desc);

                        }

                    }

                 });

            });



        },
        // 商户扣款
        cutMoneyCustomInfo:function( that ){
            var uname  = $(that).data("uname");   
            var pageNo = parseInt( $(that).data("pageno") );
            $("#cutdownmoneyCustomNum").val(uname);
            var _this = this;
            $("#cutdownmoneyCustomBtn").unbind("click").click(function(){
                 var addmoney  =  $("#cutdownmoney").val();
                 var mark = $("#cutmoneymark").val();
                 if( !addmoney )
                 {
                     $("#cutdownmoney").focus();
                     return false;
                 }
                 var pData = {
                     username:uname,
                     money:addmoney,
                     remark:mark
                 };
                 $.ajax({
                    type:"post",
                    url:"/admin/dec_money",
                    data:pData,
                    success:function( info ){
                        var code = info.code;
                        var desc = info.desc;
                        if( code == 1 )
                        {
                            _this.getReportList(pageNo);
                            $("#cutdownmoneyCustomBtn").popover("show");
                            setTimeout(function(){
                                $("#cutdownmoneyCustomBtn").popover("hide");
                                $("#cutdownmoneyCustomModal").modal("hide");
                            }, 300);

                        }
                        else
                        {
                            $("#cutdownmoneyCustomModal").modal("hide");
                            $.alert(code, desc);

                        }

                    }

                 });

            });



        }

  
   
      
      
    };





  new SetCustomInfo();

}());